/**
 * 
 */
package com.ywm8.ywm.admin.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.Cookie;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ywm8.ywm.admin.service.MenuService;
import com.ywm8.ywm.admin.service.ShopService;
import com.ywm8.ywm.domain.Admin;
import com.ywm8.ywm.domain.Menu;
import com.ywm8.ywm.domain.Shop;
import com.ywm8.ywm.utils.GetCookie;
import com.ywm8.ywm.utils.TimeFormat;
import com.ywm8.ywm.utils.ValidateUtil;

/**
 * @author wangruishun
 * @version ywm8 1.0 2014-1-20 上午11:29:20
 */
@Controller("shopAction")
@Scope("prototype")
public class ShopAction extends ActionSupport implements ServletContextAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = 3587604892797953068L;
	private ServletContext sc;

	private Shop shop;
	// 店铺图片
	private File spicture;
	private String spictureFileName;
	private String star1;
	private String star2;
	private String end1;
	private String end2;
	private String msg;
	@Resource
	private ShopService shopService;
	@Resource
	private MenuService ms;
	private String pictureFileName;
	// 菜图片
	private File picture;

	private List<Shop> shops = new ArrayList<Shop>();
	Admin admin = (Admin) ServletActionContext.getRequest().getSession()
			.getAttribute("admin");
	private Integer schoolId;
	private Integer provinceId;
	private Menu menu;

	// 添加店铺
	public String execute() {
		return "suc";
	}

	/**
	 * 添加店鋪
	 * 
	 * @return
	 */
	public String addShop() {
		// Cookie[] cookies = ServletActionContext.getRequest().getCookies();
		if (admin != null) {
			if (ValidateUtil.isValid(spictureFileName)) {
				String dir = sc.getRealPath("/upload");
				long l = System.nanoTime();
				String ext = spictureFileName.substring(spictureFileName
						.lastIndexOf("."));
				File newFile = new File(dir, l + ext);
				spicture.renameTo(newFile);
				shop.setShopPicture("/upload/" + l + ext);
				shop.setAid(admin.getId());
				shop.setProvinceId(admin.getPid());
				shop.setUnivId(admin.getUid());
				shop.setStarTime(TimeFormat.formatTimeToStr(star1, star2));
				shop.setEndTime(TimeFormat.formatTimeToStr(end1, end2));
				// if (GetCookie.getCookieValue(cookies, "uid") != null) {
				// schoolId = Integer.valueOf(GetCookie.getCookieValue(cookies,
				// "uid"));
				// }
				// if (GetCookie.getCookieValue(cookies, "pid") != null) {
				// provinceId =
				// Integer.valueOf(GetCookie.getCookieValue(cookies,
				// "pid"));
				// }
				// shop.setProvinceId(provinceId);
				// shop.setUnivId(schoolId);
				shopService.saveEntity(shop);
				return SUCCESS;
			}
			return SUCCESS;
		} else
			return "notLogin";
	}

	/**
	 * 查询店铺信息
	 */
	public String queryShopInfo() {
		if (isLogin()) {
			String queryShopSql = "from Shop sp where sp.aid = ?  and sp.isvalid=1";
			shops = shopService.findEntityByHQL(queryShopSql, admin.getId());
			if (shops.isEmpty()) {
				this.msg = "未添加店铺";
			}
			return SUCCESS;
		}
		return "notLogin";
	}

	/**
	 * 编辑店铺
	 */
	public String editShopById() {
		if (isLogin()) {
			shop = shopService.getEntity(shop.getId());
			return SUCCESS;
		} else
			return "notLogin";
	}

	public String editShop() {
		if (isLogin()) {
			if (ValidateUtil.isValid(spictureFileName)) {
				String dir = sc.getRealPath("/upload");
				long l = System.nanoTime();
				String ext = spictureFileName.substring(spictureFileName
						.lastIndexOf("."));
				File newFile = new File(dir, l + ext);
				spicture.renameTo(newFile);
				shop.setShopPicture("/upload/" + l + ext);
				shop.setStarTime(TimeFormat.formatTimeToStr(star1, star2));
				shop.setEndTime(TimeFormat.formatTimeToStr(end1, end2));
				shopService.saveOrUpdateEntity(shop);
				return SUCCESS;
			} else {
				shop.setStarTime(TimeFormat.formatTimeToStr(star1, star2));
				shop.setEndTime(TimeFormat.formatTimeToStr(end1, end2));
				shopService.saveOrUpdateEntity(shop);
				return SUCCESS;
			}
		} else
			return "notLogin";
	}

	public String deleteShopById() {
		if (isLogin()) {
			// String deleteSql =
			// "update Shop sp set sp.isvalid=0 where sp.id = ?";
			shop = shopService.getEntity(shop.getId());
			shop.setIsvalid(0);
			shopService.updateEntity(shop);
			// queryShopInfo();
			return SUCCESS;
		} else
			return "notLogin";
	}

	/**
	 * 是否登陆
	 */
	public boolean isLogin() {
		if (admin == null) {
			this.msg = "您未登陆";
			return false;
		}
		return true;
	}

	/**
	 * 根据省份id和学校id显示所有校区
	 */
	public String querySchoolByPidAndUid() {
		Cookie[] cookies = ServletActionContext.getRequest().getCookies();
		String hql = "from Shop sp where sp.pid=? and sp.uid=?";
		shops = shopService.findEntityByHQL(hql,
				GetCookie.getCookieValue(cookies, "pid"),
				GetCookie.getCookieValue(cookies, "uid"));
		return SUCCESS;

	}

	/**
	 * 在单个校区显示店铺
	 */
	public String showShopByUidAidPid() {
		String hql="from Shop sp where ";
		return SUCCESS;
	}

	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;

	}

	public String getStar1() {
		return star1;
	}

	public void setStar1(String star1) {
		this.star1 = star1;
	}

	public String getStar2() {
		return star2;
	}

	public void setStar2(String star2) {
		this.star2 = star2;
	}

	public String getEnd1() {
		return end1;
	}

	public void setEnd1(String end1) {
		this.end1 = end1;
	}

	public String getEnd2() {
		return end2;
	}

	public void setEnd2(String end2) {
		this.end2 = end2;
	}

	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	public File getSpicture() {
		return spicture;
	}

	public void setSpicture(File spicture) {
		this.spicture = spicture;
	}

	public String getSpictureFileName() {
		return spictureFileName;
	}

	public void setSpictureFileName(String spictureFileName) {
		this.spictureFileName = spictureFileName;
	}

	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg
	 *            the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	/**
	 * @return the shops
	 */
	public List<Shop> getShops() {
		return shops;
	}

	/**
	 * @param shops
	 *            the shops to set
	 */
	public void setShops(List<Shop> shops) {
		this.shops = shops;
	}

	/**
	 * @return the schoolId
	 */
	public Integer getSchoolId() {
		return schoolId;
	}

	/**
	 * @param schoolId
	 *            the schoolId to set
	 */
	public void setSchoolId(Integer schoolId) {
		this.schoolId = schoolId;
	}

	/**
	 * @return the provinceId
	 */
	public Integer getProvinceId() {
		return provinceId;
	}

	/**
	 * @param provinceId
	 *            the provinceId to set
	 */
	public void setProvinceId(Integer provinceId) {
		this.provinceId = provinceId;
	}

	/**
	 * 
	 */
	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * @param menu
	 *            the menu to set
	 */
	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	/**
	 * @return the pictureFileName
	 */
	public String getPictureFileName() {
		return pictureFileName;
	}

	/**
	 * @param pictureFileName
	 *            the pictureFileName to set
	 */
	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	/**
	 * @return the picture
	 */
	public File getPicture() {
		return picture;
	}

	/**
	 * @param picture
	 *            the picture to set
	 */
	public void setPicture(File picture) {
		this.picture = picture;
	}

}
